package main

import (
	"flag"
	"log"
	"net/http"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

func main() {

	helpPtr := flag.Bool("h", false, "this help")
	dirPtr := flag.String("d", ".", "root directoy")
	portPtr := flag.String("p", "8081", "server port")
	flag.Parse()
	if *helpPtr {
		flag.Usage()
		return
	}

	port := ":"
	port = port + *portPtr
	gin.SetMode(gin.ReleaseMode)
	router := gin.Default()
	//跨域问题
	config := cors.DefaultConfig()
	config.AllowOrigins = []string{"*"}
	config.AllowWildcard = true
	config.AllowCredentials = true
	config.AddAllowHeaders("Authorization")
	router.Use(cors.New(config))
	//静态文件服务
	router.StaticFS("/", http.Dir(*dirPtr))
	//启动服务器
	router.Run(port)
	log.Printf("Listening and serving HTTP StaticFS on %s ~\n", port)
}
